package com.ruoyi.web.service.impl;

import com.ruoyi.entity.FileDataEntity;
import com.ruoyi.entity.SubjectEntity;
import com.ruoyi.entity.ProjectEntity;
import com.ruoyi.entity.FileEntity;
import com.ruoyi.entity.SettingEntity;
import com.ruoyi.web.service.OssService;
import com.ruoyi.web.service.PneumaticService;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.SecurityUtils;

import java.io.InputStream;
import java.util.List;
import java.util.Set;

import com.ruoyi.mapper.TaskMapper;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

@Service
public class PneumaticServiceImpl implements PneumaticService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private OssService ossService;
    @Value("${minio.bucket}")
    private String pneumaticBucket;

    @Override
    public List<SubjectEntity> getSubjectList(SubjectEntity task)
    {
        Long userId = SecurityUtils.getUserId();
        task.setUserId(userId);
        return taskMapper.getSubjectList(task);
    }

    @Override
    public int deleteSubjectByIds(String[] subjectIds)
    {
        return taskMapper.deleteSubjectByIds(subjectIds);
    }

    @Override
    public int insertSubject(SubjectEntity task)
    {
        return taskMapper.insertSubject(task);
    }

    @Override
    public int updateSubject(SubjectEntity task)
    {
        return taskMapper.updateSubject(task);
    }

    @Override
    public List<ProjectEntity> getProjectList(ProjectEntity project)
    {
        return taskMapper.getProjectList(project);
    }

    @Override
    public int insertProject(ProjectEntity project)
    {
        return taskMapper.insertProject(project);
    }

    @Override
    public int deleteProjectByIds(String[] projectIds)
    {
        return taskMapper.deleteProjectByIds(projectIds);
    }

    @Override
    public int updateProject(ProjectEntity project)
    {
        return taskMapper.updateProject(project);
    }

    @Override
    public List<FileEntity> getFileList(FileEntity files)
    {
        return taskMapper.getFileList(files);
    }

    @Override
    public List<FileDataEntity> getFileData(FileDataEntity fileData)
    {
        return taskMapper.getFileData(fileData);
    }

    @Override
    public List<FileDataEntity> getFileResultData(FileDataEntity fileData)
    {
        return taskMapper.getFileResultData(fileData);
    }

    @Override
    public String getDownloadUrl(String workType, String fileName) {
        String file= workType +"/"+fileName;
        String URL=ossService.getObjectUrl(pneumaticBucket,file,2);
        return URL;
    }

    @Override
    public int deleteFileByIds(List<String> fileIds, List<String> fileNames, String directory)
    {
        for (String fileName : fileNames) {
            String objectName = directory + "/" + fileName;
            //根据图片名称删除minio图片
            ossService.removeObject(pneumaticBucket, objectName);
        }
        return taskMapper.deleteFileByIds(fileIds);
    }
    @Override
    public List<FileDataEntity> globalSearch(List<FileDataEntity> pneumaticList)
    {
        return taskMapper.globalSearch(pneumaticList);
    }
    @Override
    public List<FileEntity> getFileListByIds(Set<String> fileIds, String fileType)
    {
        return taskMapper.getFileListByIds(fileIds, fileType);
    }

    @Override
    public int updateUserSubject(String userId, String subjectIds)
    {
        return taskMapper.updateUserSubject(userId, subjectIds);
    }

    @Override
    public List<SubjectEntity> authSubjectList(String userId)
    {
        return taskMapper.authSubjectList(userId);
    }

    @Override
    public List<SettingEntity> getSettingsInfo(SettingEntity settings)
    {
        return taskMapper.getSettingsInfo(settings);
    }
    public void addSetting(MultipartFile file, String LoginTitle, Integer isActive) {
        try {
            @Cleanup
            InputStream inputStream = file.getInputStream();
            String fileName = UUID.randomUUID() + file.getOriginalFilename();
            ossService.putObject(pneumaticBucket, fileName,inputStream);
            taskMapper.addSetting(fileName, LoginTitle, isActive);
            return;
        } catch (Exception e) {
            System.out.println(e);
        }

    }
    public void activate(Integer id) {
        taskMapper.activate(id);
    }
    @Override
    public int removeSetting(Integer id, String fileName)
    {
        //根据图片名称删除minio图片
        ossService.removeObject(pneumaticBucket, fileName);

        return taskMapper.removeSetting(id);
    }
    @Override
    public SettingEntity getSetting() {
        SettingEntity detail;
        detail = taskMapper.getSetting();
        if (detail != null) {
            String file= detail.getLoginImage();
            String URL = ossService.getObjectUrl(pneumaticBucket,file,2);
            detail.setLoginImage(URL);
        }
        return detail;
    }
}
